package com.alex.server.service.impl;

import com.alex.server.domain.${Domain};
import com.alex.server.dto.${Domain}Dto;
import com.alex.server.dto.PageDto;
import com.alex.server.mapper.${Domain}Mapper;
import com.alex.server.service.I${Domain}Service;
import com.alex.server.util.CopyUtil;
import com.alex.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class ${Domain}ServiceImpl implements I${Domain}Service {

    @Autowired
    private ${Domain}Mapper mapper;

    @Override
    public PageDto list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        List<${Domain}> ${domain}List = mapper.selectByExample(null);
        PageInfo<${Domain}> pageInfo = new PageInfo<>(${domain}List);
        pageDto.setTotal(pageInfo.getTotal());
        List<${Domain}Dto> ${domain}DtoList = CopyUtil.copyList(${domain}List, ${Domain}Dto.class);
        pageDto.setList(${domain}DtoList);
        return pageDto;
    }

    @Override
    public void save(${Domain}Dto ${domain}Dto) {
        ${Domain} ${domain} = CopyUtil.copy(${domain}Dto, ${Domain}.class);
        if (StringUtils.isEmpty(${domain}Dto.getId())){
            this.insert(${domain});
        } else {
            this.update(${domain});
        }
    }

    @Override
    public void insert(${Domain} ${domain}) {
        ${domain}.setId(UuidUtil.getShortUuid());
        mapper.insert(${domain});
    }

    @Override
    public void update(${Domain} ${domain}) {
        mapper.updateByPrimaryKey(${domain});
    }

    @Override
    public void delete(String id) {
        mapper.deleteByPrimaryKey(id);
    }

}